package controller;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.Domain;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

public class RemoveServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        if (user != null && userService.isUserAdmin()) {
        	String name = req.getParameter("name");
    		
    		try {
    			ObjectifyService.register(Domain.class);
    		} catch (Exception e) {
    			// TODO: handle exception
    		}
    		Objectify objectify = ObjectifyService.begin();
    		
//    		try {
    			Domain domain = objectify.get(new Key<Domain>(Domain.class, name));
    			
    			domain.setStatus(10);
    			
    			objectify.put(domain);
//    		} catch (Exception e) {
//    			resp.sendError(resp.SC_NOT_FOUND,"Content not removed!");
//    		}
        	
        } else {
            resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
        }
		
		
		
	}
}
